package com.jueban.controller;

import com.jueban.Dto.*;
import com.jueban.Vo.ResultVo;
import com.jueban.Vo.UserVo;
import com.jueban.Vo.UsersVo;
import com.jueban.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/user")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }


    //查询是否有该名称的用户存在
    @RequestMapping(value = "/hasName", method = RequestMethod.POST)
    public ResultVo hasName(@RequestBody String username) {
        System.out.println(username);
        return userService.hasUsername(username);
    }

    //注册一个账号
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResultVo register(@RequestBody @Valid UserDto userDto) {
        return userService.register(userDto);
    }

    //修改账户密码
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    public ResultVo resetPassword(@RequestBody @Valid ResetPasswordDto resetPasswordDto) {
        return userService.resetPassword(resetPasswordDto);
    }

    //修改用户资料
    @RequestMapping(value = "/changeInfo", method = RequestMethod.POST)
    public ResultVo changeInfo(@RequestBody @Valid UserDto userDto) {
        ResultVo resultVo;
        if("".equals(userDto.getPassword()) || userDto.getPassword() == null)
        {
            resultVo = userService.updateUserNotPassword(userDto);
        }
        else
        {
            resultVo = userService.updateUser(userDto);
        }
        return resultVo;
    }

    //批量查询用户
    @PostMapping("/findAll")
    public UsersVo findAll(@RequestBody @Valid UserQueryDto userQueryDto)
    {
        return userService.findAllUser(userQueryDto);
    }

    //修改用户职位 admin
    @RequestMapping(value = "/changeJob", method = RequestMethod.POST)
    public ResultVo changeJob(@RequestBody @Valid JobTypeDto jobTypeDto) {
        return userService.changeJob(jobTypeDto);
    }


    //批准一个账号注册
    //废除一个账号
    @RequestMapping(value = "/setRegister", method = RequestMethod.POST)
    public ResultVo setRegister(@RequestBody @Valid RegisterDto registerDto) {
        return userService.setRegister(registerDto);
    }


    //登陆


    //注销


    //查询未批准的账号
    @RequestMapping(value = "/unregisterAccount", method = RequestMethod.POST)
    public UsersVo getUnregisterAccount(@RequestBody @Valid UnregisteredUserQueryDto pageDto) {
        return userService.getUnregister(pageDto);
    }


    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public UserVo getInfo(){
        return userService.getInfo();
    }


}
